﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PcArduinoControl
{
	public class DigitalAnalogOutputPin :
		DigitalOutputPin, IAnalogPin
	{
		// TODO: Switch to sbyte where we need to so we can actually send negative values without making the user use unchecked and stuff.

		internal event AnalogOutputChangeHandler AnalogValueChanged;

		internal bool IsUsingAnalog = false; // true if analog was set last, false if digital was set last
		byte mAnalogValue;

		public byte AnalogValue
		{
			get
			{
				return mAnalogValue;
			}
			set
			{
				mAnalogValue = value;
				IsUsingAnalog = true;
				OnAnalogValueChanged();
			}
		}

		protected override void DigitalValueSet()
		{
			base.DigitalValueSet();
			IsUsingAnalog = false;
		}

		public DigitalAnalogOutputPin(byte pin) :
			base(pin)
		{ }

		void OnAnalogValueChanged()
		{
			var valueChanged = AnalogValueChanged;
			if (valueChanged != null)
				valueChanged.Invoke(Number, mAnalogValue);
		}
	}
}
